# -*- encoding: utf-8 -*-
"""
File Name: monitor_zabbix
Description:
Author : 'lirui'
date: 10/24/2024
Contact: marjey_lee@163.com
Change Activity:
10/24/2024:
"""
import ll
from projects.monitoring.common.utils.redis_keys_util import RedisKeysUtil
from projects.monitoring.common.workflow.monitoring_pipline_interface import MonitorPipLineInterface
from projects.monitoring.zabbix_monitoring.utils.format_problems import format_zabbix_alarms
from projects.monitoring.zabbix_monitoring.utils.monitor_pipline_util import load_zabbix_docker_info_from_cmdb, get_one_zabbix_server_problems


class ZabbixMonitorPipeline(MonitorPipLineInterface):
    _MONITORING_TYPE = RedisKeysUtil.get_internal_zabbix_publisher_keys()

    def __init__(self, monitor_interval):
        super().__init__(self._MONITORING_TYPE, monitor_interval)
        self.zabbix_login_info = load_zabbix_docker_info_from_cmdb()

    def _load_original_alarms(self):
        ll.get_logger().info('Start to load problems from zabbix server.')
        problems_summary = []
        for info in self.zabbix_login_info:
            problems = get_one_zabbix_server_problems(info)
            problems_summary.extend(problems)
            ll.get_logger().debug(f'load {info["docker_server_ip"]} problems finished, total problems count {len(problems)}.')
        ll.get_logger().info(
            f'Load problems from zabbix server finished, total problems count {len(problems_summary)}.'
        )
        return problems_summary

    def _format_alarms(self, original_alarms):
        return format_zabbix_alarms(original_alarms)
